﻿using System;

using WinFormsApp1.MVC.Model;

namespace WinFormsApp1.MVI.MVIER
{
	public class MviController
	{
		public event Action<State> StateChanged;
		private State _state;

		public MviController()
		{
			_state = new State { Person = new Person() };
		}

		public void ProcessIntent(Intent intent, object data = null)
		{
			switch (intent)
			{
				case Intent.UpdateName:
					_state.Person.Name = data as string;
					break;
				case Intent.UpdateAge:
					_state.Person.Age = (int)data;
					break;
				case Intent.ShowMessage:
					_state.Message = $"创建: {_state.Person.Name}, 年龄: {_state.Person.Age}";
					break;
			}
			StateChanged?.Invoke(_state);
		}

		internal void updatemodel()
		{
			Task.Run(() =>
			{
				Thread.Sleep(3000);
				ProcessIntent(Intent.UpdateName, "已修改"+DateTime.Now);
			});
		}
	}

}
